#!/bin/bash
SCRIPT_BASE_DIR=$( dirname "${BASH_SOURCE[0]}" )
source "${SCRIPT_BASE_DIR}/common/fs_utils.sh"
source "${SCRIPT_BASE_DIR}/common/cs_utils.sh"

root="."
exts=(c h cpp hpp cc hxx cxx td def py)
fnames=(CMakeLists.txt)
excludeNames=(build test .pc)

cmd=$( basename "$0" )

function usage() {
	echo "Usage: ${cmd} [-h | -t] [<root_dir>]"
    echo "      对<root_dir>目录下的所有扩展名为"${exts[@]}"和所有的${fnames[@]}文件做cscope索引"
    echo "      <root_dir>: 默认为当前目录"
    echo "      -h: 输出此信息"
    echo "      -t: 索引包括test目录"
	exit 1
}

sCnt=0
while getopts "ht" arg
do
    sCnt=$(( $sCnt + 1 ))
    case "${arg}" in 
        h)
            usage
            ;;
        t)
            # 从excludeNames里删除test
            unset excludeNames[1]
            shift
            ;;
        *)
            usage
            ;;
    esac
done

shift ${sCnt}

if [ -n "$1" ]
then
    root="$1"
fi

# 传递数组变量名字
build_cs_file_list "${root}" exts fnames excludeNames

{
    echo "Build cscope DB in ${root} ..."
    cd ${root}
    cscope -Rbqk
}

echo "Done."

